package main

import (
	"errors"
	"fmt"
)

func main() {
	x, err := NewBuilderA().WithBase("name", 1).WithLimit(4, 1).WithXY(4, 5).Build()
	if nil != err {
		panic(err)
	}
	println(x)
	fmt.Println(x)
}


/* v1是最简单的构造 */
type A struct {
	Name string
	HP   int

	// 存在依赖限制，需要校验
	Max int
	Min int

	// 存在依赖限制，需要合并完成了对象才有效
	Xlabel int
	Ylabel int
}

type BuildA struct {
	// builder Builder
	AObj A
	err  error
}

func NewBuilderA() BuildA {
	o := A{}
	o.Name = "默认"
	return BuildA{
		AObj: o,
		err:  nil,
	}
}

func (b BuildA) WithBase(n string, h int) BuildA {
	b.AObj.Name = n
	b.AObj.HP = h
	return b
}

func (b BuildA) WithLimit(max, min int) BuildA {
	if b.err != nil {
		return b
	}
	if max < min {
		b.err = errors.New("max < min")
	}
	b.AObj.Max = max
	b.AObj.Min = min
	return b
}

func (b BuildA) WithXY(x, y int) BuildA {
	if b.err != nil {
		return b
	}

	b.AObj.Xlabel = x
	b.AObj.Ylabel = y
	return b
}

func (b BuildA) Build() (*A, error) {
	if b.err != nil {
		return nil, b.err
	}
	// 增加校验逻辑

	return &b.AObj, nil
}
